package edu.dailicious.tapestryboard.pages.grid;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Retain;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import edu.dailicious.tapestryboard.bean.Artist;

public class GridAddCheckbox {
	@Property
	private Artist artist;
	
	@Persist
	private List<Artist> artistList;
	
	@Retain
	private BeanModel<Artist> artistModel;
	@Inject
	private BeanModelSource beanModelSource;
	@Inject
	private ComponentResources resources;
	
	private boolean delete;
	private List<Artist> deletedList;
	
	@Component
	private Form artistForm;
	
	public boolean isDelete() {
		return delete;
	}
	
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(artist);
		}
		this.delete = delete;
	}	
	
	public List<Artist> getDeletedList() {
		if(deletedList == null){
			deletedList = new ArrayList<Artist>();
		}
		return deletedList;
	}
	
	public List<Artist> getArtistList() {
		if(artistList==null || artistList.size()==0){
			artistList = new ArrayList<Artist>();
			artistList.add(new Artist("001", "Dwi Ardi", "Irawan", new Date()));
			artistList.add(new Artist("002", "Britney", "Spears", new Date()));
			artistList.add(new Artist("003", "Jesse", "McCartney", new Date()));
			artistList.add(new Artist("004", "David", "Bechkam", new Date()));
			artistList.add(new Artist("005", "Mandy", "Moore", new Date()));
			artistList.add(new Artist("006", "Cinta", "laura", new Date()));
		}
		
		return artistList;
	}
	
	public void setArtistList(List<Artist> artistList) {
		this.artistList = artistList;
	}

	public BeanModel<Artist> getArtistModel() {
		return artistModel;
	}

	void pageLoaded(){
		artistModel = beanModelSource.createDisplayModel(Artist.class, resources.getMessages());
		artistModel.add("actions", null).label("Delete");
	}
	
	void onSuccessFromArtistForm(){
		for(Artist _artist : getDeletedList()){
			System.out.println("hapus : " + _artist.getFirstName());
			getArtistList().remove(_artist);
				
		}
		
		setArtistList(getArtistList());
		for(Artist _artist2 : getArtistList()){
			System.out.println(_artist2.getFirstName());
				
		}
		
		
	}
	
}
